package Demo_zhuanshu01.X1;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 2、两个存储分别向同一个账号存3000元,每次存1000,存三次,每次存完打印账号余额
 */
public class Day01 {
    public static void main(String[] args) {
        Day01 runnable = new Day01(0);
        Custody custody1 = new Custody(runnable);
        Custody custody2 = new Custody(runnable);
        custody1.setName("甲");
        custody2.setName("乙");
        custody1.start();
        custody2.start();

    }
        private double balance;

    public Day01(double balance) {
        this.balance = balance;
    }
    public void deposit(double atm){
        if (atm>0){
            balance+=atm;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+"存钱，当前余额为："+balance);
        }
    }
}
class Custody extends Thread{
    private Day01 acc;

    public Custody(Day01 acc) {
        this.acc = acc;
    }

    private static ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        lock.lock();
        for (int i = 0; i <3; i++) {
            acc.deposit(1000);
        }
        lock.unlock();
    }
}
